package com.ruoyi.business.service.impl;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.business.domain.Laserpowerdetectorhis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.LaserheaderhisMapper;
import com.ruoyi.business.domain.Laserheaderhis;
import com.ruoyi.business.service.ILaserheaderhisService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author DL
 * @date 2025-07-08
 */
@Service
public class LaserheaderhisServiceImpl implements ILaserheaderhisService 
{
    @Autowired
    private LaserheaderhisMapper laserheaderhisMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param ID 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Laserheaderhis selectLaserheaderhisByID(Long ID)
    {
        return laserheaderhisMapper.selectLaserheaderhisByID(ID);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param laserheaderhis 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Laserheaderhis> selectLaserheaderhisList(Laserheaderhis laserheaderhis)
    {
        return laserheaderhisMapper.selectLaserheaderhisList(laserheaderhis);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param laserheaderhis 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertLaserheaderhis(Laserheaderhis laserheaderhis)
    {
        return laserheaderhisMapper.insertLaserheaderhis(laserheaderhis);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param laserheaderhis 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateLaserheaderhis(Laserheaderhis laserheaderhis)
    {
        return laserheaderhisMapper.updateLaserheaderhis(laserheaderhis);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param IDs 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLaserheaderhisByIDs(Long[] IDs)
    {
        return laserheaderhisMapper.deleteLaserheaderhisByIDs(IDs);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param ID 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteLaserheaderhisByID(Long ID)
    {
        return laserheaderhisMapper.deleteLaserheaderhisByID(ID);
    }

    @Override
    public Map<String, Object> getLaserheaderhisDataBetween(LocalDateTime startTime, LocalDateTime endTime) {
        // 查询数据库获取数据
        List<Laserheaderhis> dataList = laserheaderhisMapper.findByTimestampBetween(startTime, endTime);

        // 处理数据格式
        Map<String, Object> result = new HashMap<>();
        String[] xdata = new String[dataList.size()];
        double[] OpticalFibreFlowRate = new double[dataList.size()];
        double[] OpticalFibreTemp = new double[dataList.size()];
        double[] QBHFlowRate = new double[dataList.size()];
        double[] QBHTemp = new double[dataList.size()];
        double[] LensFlowRate = new double[dataList.size()];
        double[] LensTemp = new double[dataList.size()];
        double[] ProtectHeadFlowRate = new double[dataList.size()];
        double[] ProtectHeadTemp = new double[dataList.size()];

        // 定义格式化器
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

        for (int i = 0; i < dataList.size(); i++) {
            Laserheaderhis data = dataList.get(i);
            xdata[i] = sdf.format(data.getEventTime());
            OpticalFibreFlowRate[i] = data.getOpticalFibreFlowRate() != null ? data.getOpticalFibreFlowRate().doubleValue() : 0.0;
            OpticalFibreTemp[i] = data.getOpticalFibreTemp() != null ? data.getOpticalFibreTemp().doubleValue() : 0.0;
            QBHFlowRate[i] = data.getQBHFlowRate() != null ? data.getQBHFlowRate().doubleValue() : 0.0;
            QBHTemp[i] = data.getQBHTemp() != null ? data.getQBHTemp().doubleValue() : 0.0;
            LensFlowRate[i] = data.getLensFlowRate() != null ? data.getLensFlowRate().doubleValue() : 0.0;
            LensTemp[i] = data.getLensTemp() != null ? data.getLensTemp().doubleValue() : 0.0;
            ProtectHeadFlowRate[i] = data.getProtectHeadFlowRate() != null ? data.getProtectHeadFlowRate().doubleValue() : 0.0;
            ProtectHeadTemp[i] = data.getProtectHeadTemp() != null ? data.getProtectHeadTemp().doubleValue() : 0.0;
        }

        result.put("xdata", xdata);
        result.put("OpticalFibreFlowRate", OpticalFibreFlowRate);
        result.put("OpticalFibreTemp", OpticalFibreTemp);
        result.put("QBHFlowRate", QBHFlowRate);
        result.put("QBHTemp", QBHTemp);
        result.put("LensFlowRate", LensFlowRate);
        result.put("LensTemp", LensTemp);
        result.put("ProtectHeadFlowRate", ProtectHeadFlowRate);
        result.put("ProtectHeadTemp", ProtectHeadTemp);

        return result;
    }
}
